package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Goal;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class GoalDao {
	private DBHelp<Goal> db=new DBHelp<Goal>();
	private TaskDao tdao=new TaskDao();

	public List<Goal> findAllByprojectId(String id) {
		String sql="SELECT id,gname,gdescription,t_project_id FROM t_goal WHERE t_project_id=?";
		return db.executeQueryForList(sql,new GoalRowMapper(),id);
	}
	public Goal findById(String goalid) {
		String sql="SELECT id,gname,gdescription,t_project_id FROM t_goal WHERE id=?";
		return db.executeQueryForObject(sql, new GoalRowMapper(), goalid);
	}
	public boolean save(Goal goal) {
		String sql="INSERT INTO t_goal(id,gname,gdescription,t_project_id) VALUES(?,?,?,?)";
		return db.executeSQL(sql, goal.getId(),goal.getGname(),goal.getGdescription(),goal.getT_project_id());
		
	}
	public boolean update(String goalid, String name, String desc, String projectid) {
		String sql="UPDATE t_goal SET gname=?,gdescription=?,t_project_id=? WHERE id=?";
		return db.executeSQL(sql,name,desc,projectid,goalid);
	}
	
	private class GoalRowMapper implements RowMapper<Goal>{

		public Goal mapRow(ResultSet rs) throws SQLException {
			Goal goal=new Goal();
			goal.setId(rs.getString("id"));
			goal.setGname(rs.getString("gname"));
			goal.setGdescription(rs.getString("gdescription"));
			goal.setT_project_id(rs.getString("t_project_id"));
			return goal;
		}
		
	}

	public boolean delByGoalId(String goalid) {
		tdao.delByGoalId(goalid);
		String sql="delete from t_goal where id=?";
		return db.executeSQL(sql, goalid);
	}

	
	
}
